<?php

/**
 * 定义 Controller_BoProduct 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241027
 */
FLEA::loadClass('Controller_BoBase');

class Controller_BoAttendance extends Controller_BoBase {

    var $_modelClasses;
    var $_tableFormula;
    var $_tableUser;
    var $_tableAttendance;
    var $_tableClasses;
    var $access;
    var $_helperFunction;
    var $dept_id;
    var $position_id;
    var $edu_id;
    var $dbo;

    //构造函数
    function Controller_BoAttendance() {
        parent::Controller_BoBase();
        $this->access = USER_ADMIN;
        //用户角色权限控制
        if (!$this->checkDispatcher($this->access)) {
            redirect(url('BoDashboard', 'ErrorIn'));
        }
        $this->_modelClasses = & FLEA::getSingleton('Model_Categories');
        $this->_tableFormula = & FLEA::getSingleton('Table_Formula');
        $this->_tableUser = & FLEA::getSingleton('Table_User');
        $this->_tableAttendance = & FLEA::getSingleton('Table_Attendance');
        $this->_tableClasses = & FLEA::getSingleton('Table_Categories');
        $this->dept_id = DEPARTMENT_CLASS;
        $this->position_id = POSITION_CLASS;
        $this->edu_id = EDUCATION_CLASS;
        $this->_helperFunction = & FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }

    function actionIndex() {
        $dept = $this->_modelClasses->getClass($this->dept_id);
        $deptclasses = $this->_modelClasses->getSubClasses($dept);
        //dump($this->dbo->log);
        $listTitle = "工资统计";
        include(TPL_DIR . '/attendance.php');
    }

    function actionHistory() {
        $yesr = $_POST['year'];
        $month = $_POST['month'];
        $dept = $this->_modelClasses->getClass($this->dept_id);
        $deptclasses = $this->_modelClasses->getSubClasses($dept);
        $sql = 'select sum(all_pay) as total,a.year,a.month,c.name as department,dateline,a.updated,a.dept_id
from iteam_attendance a
inner join iteam_categories c on c.class_id=a.dept_id
where attendance_id>0
';
        if ($_REQUEST['department'])
            $sql.= 'and a.dept_id=' . $_REQUEST['department'] . '
';
        if ($yesr)
            $sql.= 'and a.year=' . $yesr . '
';
        if ($month)
            $sql.= 'and a.month=' . $month . '
';
        $sql.= 'group by dept_id,year,month
order by year desc,month desc';

        $table = & $this->_tableAttendance;
        $rowset = $table->findBySql($sql, 20);
//		dump($this->dbo->log);
//		var_dump($rowset);
        $listTitle = "工资统计列表";
        include(TPL_DIR . '/attendanceList.php');
    }

    function actionAttendanceDetail() {
        $yesr = $_REQUEST['year'];
        $month = $_REQUEST['month'];
        $dept_id = $_REQUEST['dept_id'];
        $department = $_REQUEST['department'];
        $fields = array();
        $fields['year'] = $yesr;
        $fields['month'] = $month;
        $fields['dept_id'] = $dept_id;
        $table = & $this->_tableAttendance;
        $rowset = $table->findAll($fields);
//		dump($this->dbo->log);
//		var_dump($rowset);
        $listTitle = $department . '部门' . $yesr . '年' . $month . '月员工工资列表';
        include(TPL_DIR . '/attendanceDetail.php');
    }

    //增加用户
    function addUser($excelData) {
        $data['code'] = $excelData[1];
        $data['name'] = $excelData[0];
        $eduction = $this->_tableClasses->findByField('name', $excelData['4']);
        if (!$eduction) {
            echo $excelData[1] . '学历不存在，请检查表格';
            exit;
        }
        $data['education'] = $eduction['class_id'];
        $department = $this->_tableClasses->findByField('name', $excelData['2']);
        if (!$department) {
            echo $excelData[1] . '部门不存在，请检查表格';
            exit;
        }
        $data['dept_id'] = $department['class_id'];
        $position = $this->_tableClasses->findByField('name', $excelData['3']);
        if (!$position) {
            echo $excelData[1] . '职位不存在，请检查表格';
            exit;
        }
        $data['position_id'] = $position['class_id'];
        if (is_numeric($excelData[5]))
            $excelData[5] = $this->excelTime($excelData[5]);
        $data['employ_time'] = $excelData[5];
        if (is_numeric($excelData[6]))
            $excelData[6] = $this->excelTime($excelData[6]);
        $data['permanent_time'] = $excelData[6];
        $data['dateline'] = time();
        $data['updated'] = $data['dateline'];
        return $this->_tableUser->save($data);
    }

    //新员工用老员工编号
    function addSpecialUser($user, $excelData) {
        //将老员工的状态置成2
        $data = array();
        $data['uid'] = $user['uid'];
        $data['status'] = 2;
        $this->_tableUser->save($data);
        $uid = $this->addUser($excelData);
        return $this->_tableUser->findByField('uid', $uid);
    }

    //更新员工转正时间
    function updateUser($user, $upData) {
        $data = $upData;
        $data['uid'] = $user['uid'];
        $this->_tableUser->save($data);
        return $this->_tableUser->findByField('uid', $user['uid']);
    }

    function excelTime($date, $time=false) {
        if (is_numeric($date)) {
            $jd = GregorianToJD(1, 1, 1970);
            $gregorian = JDToGregorian($jd + intval($date) - 25569);
            $date = explode('/', $gregorian);
            $date_str = str_pad($date[2], 4, '0', STR_PAD_LEFT)
                    . "-" . str_pad($date[0], 2, '0', STR_PAD_LEFT)
                    . "-" . str_pad($date[1], 2, '0', STR_PAD_LEFT)
                    . ($time ? " 00:00:00" : '');
            return $date_str;
        }
        return $date;
    }

    //导入考勤表计算工资
    function actionImport() {
        $year = $_POST['year'];
        $month = $_POST['month'];
        $deadline = $year . '-' . $month;
        $department = $_POST['department'];
        if (!$year || !$month || !$department) {
            echo "请填写所有项";
            exit;
        }
        $errorMessage = $this->_uploadPicture();
        if (!$errorMessage == "" && is_array($errorMessage)) {
            //取的所有职位信息
            $posi = $this->_modelClasses->getClass($this->position_id);
            $positions = $this->_modelClasses->getSubClasses($posi);
            $posiArr = array();
            foreach ($positions as $position) {
                $posiArr[$position['name']] = $position['class_id'];
            }

            //读取上传文件内容
            $file = $errorMessage['filename'];
            $PHPExcel = $this->readTemplate($file, UPLOAD_DIR . DS);
            $ActSheet = $PHPExcel->getActiveSheet();

            /*             * 取得一共有多少列 */
            $allColumn = $ActSheet->getHighestColumn();
            /*             * 取得一共有多少行 */
            $allRow = $ActSheet->getHighestRow();
            $columnArray = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

            $dataArr = array();
            for ($currentRow = 3; $currentRow <= $allRow; $currentRow++) {
                $dataRow = array();
                for ($j = 0; $j < count($columnArray); $j++) {
                    $column = $columnArray[$j];
                    $rowValue = $ActSheet->getCell($column . $currentRow)->getValue();
                    //如果值来自公式，则需要特殊处理
                    if ($j >= 7 && $j <= 18) {
                        if (!is_numeric($rowValue))
                            $rowValue = $ActSheet->getCell($column . $currentRow)->getCalculatedValue();
                    }
                    $dataRow[] = $rowValue;
                    unset($rowValue);
                    if ($column == $allColumn)
                        break;
                }
                $dataArr[] = $dataRow;
                unset($dataRow);
            }

            foreach ($dataArr as $dataArr2) {

                if (!$dataArr2[0])
                    continue;
                if ($dataArr2[2] != $department) {
                    echo "所选部门与上传文件不符";
                    break;
                }
                $user = $this->_tableUser->findAll(array('code' => $dataArr2[1], 'status' => 1));
                if (is_array($user))
                    $user = $user[0];
                if (!$user) {
                    //$msg.= 'Code为'.$dataArr2[1].'的员工不存在，请核查<br>';
                    //continue;
                    $uid = $this->addUser($dataArr2);
                    $user = $this->_tableUser->findByField('uid', $uid);
                } else {
                    if (is_numeric($dataArr2[5]))
                        $dataArr2[5] = $this->excelTime($dataArr2[5]);
                    if (is_numeric($dataArr2[6]))
                        $dataArr2[6] = $this->excelTime($dataArr2[6]);
                    //处理新员工用老员工编号的问题
                    if ($dataArr2[0] != $user['name'] && strtotime($dataArr2[5]) != strtotime($user['employ_time']))
                        $user = $this->addSpecialUser($user, $dataArr2);
                    //自动更新转正记录,职位变化，养老保险变化
                    $upField = array();
                    if (strtotime($dataArr2[6]) != strtotime($user['permanent_time']))
                        $upField['permanent_time'] = date('Y-m-d', strtotime($dataArr2[6]));
                    if ($posiArr[$dataArr2[3]] != $user['position_id'])
                        $upField['position_id'] = $posiArr[$dataArr2[3]];
                    if ((int) $dataArr2[19] != (int) $user['endowment'])
                        $upField['endowment'] = (int) $dataArr2[19];
                    if ((int) $dataArr2[20] != (int) $user['medicare']) {
                        $upField['medicare'] = (int) $dataArr2[20];
                    }
                    if ($dataArr2[0] == $user['name'] && strtotime($dataArr2[5]) == strtotime($user['employ_time']) && $upField) {
                        $user = $this->updateUser($user, $upField);
                    }
                    unset($upField);
                }

                $fields = array();
                $fields['uid'] = $user['uid'];
                $fields['year'] = $year;
                $fields['month'] = $month;
                $record = $this->_tableAttendance->find($fields);
                unset($fields);

                $data = array();
                if ($record)
                    $data['attendance_id'] = (int) $record['attendance_id'];
                else {
                    $data['attendance_id'] = '';
                    $data['dateline'] = time();
                }
                unset($record);

                //计算入职月数和转正月数
                //修正系数，15号之前入职的月数+1
                if (substr($user['employ_time'], 8, 2) <= 15)
                    $employType = 1;
                $diffMonth = $this->DiffDate(substr($user['employ_time'], 0, 7), $deadline); //入职月数
                $diffMonth = $diffMonth[0] * 12 + $diffMonth[1];
                $diffMonth += $employType;
                //处理修正月数，产假和辞工回来的
                $diffMonth += $user['correction_month'];
                unset($employType);
                //处理工龄工资新公式情况，截止到20010301
                $diffMonthNew = $this->DiffDate('2011-02', $deadline); //201102到现在的月份,因为是3月份呢截止，所以要算到2月的月份
                $diffMonthNew = $diffMonthNew[0] * 12 + $diffMonthNew[1];



                //转正月数，如果转正月数不存在则不用公式计算
                //修正系数，15号之前转正的转正月数+1
                if (substr($user['permanent_time'], 8, 2) <= 15)
                    $permanentType = 1;
                //处理计算工资时间小于转正时间的特殊情况
                $mk_permanent_time = mktime(0, 0, 0, substr($user['permanent_time'], 5, 2), 1, substr($user['permanent_time'], 0, 4));
                $mk_deadline = mktime(0, 0, 0, $month, 1, $year);
                if ($user['permanent_time'] && $user['permanent_time'] != '0000-00-00' && $mk_deadline >= $mk_permanent_time) {
                    $diffMonth2 = $this->DiffDate(substr($user['permanent_time'], 0, 7), $deadline);
                    $diffMonth2 = $diffMonth2[0] * 12 + $diffMonth2[1];
                    $diffMonth2 += $permanentType;
                    //处理修正月数，产假和辞工回来的
                    $diffMonth2 += $user['correction_month'];
                }else
                    $diffMonth2 = 0;
                unset($permanentType);

                $data['uid'] = $user['uid'];
                $data['dept_id'] = $user['dept_id'];
                $data['position_id'] = $user['position_id'];
                $data['edu_id'] = $user['education'];
                $data['year'] = $year;
                $data['month'] = $month;
                $data['employ_month'] = $diffMonth;
                $data['permanent_month'] = $diffMonth2;
                $data['work_days'] = $dataArr2[9];
                $data['night'] = $dataArr2[10] + $dataArr2[11];
                $data['personal_leave'] = $dataArr2[12] + $dataArr2[13];
                $data['late'] = $dataArr2[16];
                $data['forget_clock'] = $dataArr2[17];
                $data['overtime'] = $dataArr2[18];
                $data['bonus'] = $dataArr2[21];
                $data['description'] = $dataArr2[22];
                
                //矿工的数据导入，以小时为单位，把迟到当旷工用
                $data['absenteeism'] = $dataArr2[14];
                

                unset($diffMonth2);

                //开始计算工资
                //基本工资
                if ($data['permanent_month'] > 0) {
                    //已经转正的员工
                    $base_pay = $this->formulaPay(101, $user['dept_id'], $user['education']);
                    if (!$base_pay) {
                        $msg.= 'Code为' . $dataArr2[1] . '的职工相关学历条件缺失，无法计算基本工资<br>';
                        break;
                    }
                    $data['base_pay'] = $base_pay['money'];
                } else {
                    //试用期员工
                    switch ($department) {
                        case 'SI':
                            $data['base_pay'] = 800;
                            break;
                        case 'IP':
                            $data['base_pay'] = 900;
                            break;
                        default:
                            $data['base_pay'] = 1000;
                            break;
                    }
                }
                unset($base_pay);

                //职位津贴
                $position_pay = $this->formulaPay(102, $user['dept_id'], $user['position_id']);
                //不存在做0处理
                /* if(!$position_pay){
                  $msg.= 'Code为'.$dataArr2[0].'的职工相关职位条件缺失，无法计算职位津贴<br>';
                  break;
                  } */
                $data['position_pay'] = $position_pay['money'];
                unset($position_pay);

                //工龄工资
                $seniority_pay = $this->formulaPay(103, 0, 0);
                if (!$seniority_pay) {
                    $msg.= '工龄工资规则缺失，无法计算<br>';
                    break;
                }
                //工龄工资改革前的工龄工资
                if ($diffMonth >= 1)
                    $data['seniority_pay'] = 25 * floor(($diffMonth - $diffMonthNew - 1) / 6);
                else
                    $data['seniority_pay'] = 0;
                if ($data['seniority_pay'] < 0)
                    $data['seniority_pay'] = 0;
                //工龄工资改革后的工龄工资				
                if ($seniority_pay['money'] * floor((($diffMonth - $diffMonthNew - 1) % 6 + $diffMonthNew) / 12) >= 0)
                    $data['seniority_pay'] = $data['seniority_pay'] + $seniority_pay['money'] * floor((($diffMonth - $diffMonthNew - 1) % 6 + $diffMonthNew) / 12);
                unset($seniority_pay);

                //保险津贴
                if ($data['permanent_month'] > 0) {
                    $data['insurance_pay'] = 350 - $user['endowment'] * 250 - $user['medicare'] * 100;
                }else
                    $data['insurance_pay'] = 0;


                //加班工资
                $data['overtime_pay'] = $data['overtime'] * ($data['base_pay'] + $data['position_pay'] + $data['seniority_pay']) / 24;


                //晚班津贴
                $fields = array();
                $fields['type'] = 104;
                $fields['department'] = $user['dept_id'];
                $night_formula = $this->_tableFormula->findAll($fields, 'month desc');
                unset($fields);
                if (!$night_formula) {
                    $msg.= '晚班津贴规则缺失，无法计算<br>';
                    break;
                }
                $night_pay_ceil = 0;
                foreach ($night_formula as $nightRow) {
                    if ($diffMonth < $nightRow['month'])
                        $night_pay_ceil = $nightRow['money'];
                    else
                        break;
                }
                $data['night_pay'] = $night_pay_ceil * $data['night'];

                //其他奖金，固定的每月奖金
                $other_bonus = $this->formulaPay(999, 0, $user['uid']);
                if (!$other_bonus)
                    $data['other_bonus'] = 0;
                else
                    $data['other_bonus'] = $other_bonus['money'];
                unset($other_bonus);

                //缺勤扣除,这个是负数
                $data['absence_pay'] = -round(($data['base_pay'] + $data['seniority_pay'] + $data['position_pay']) * (($data['personal_leave'] + $data['absenteeism'] * 3) / 8) / 24);

                //全勤奖取消了，纳入基本工资了
//				if($data['personal_leave']>=8)$fulltype = true;
//				
//				if($department=='SI'){
//					if(!$fulltype)$full_pay = 100;
//				}else{
//					$diffMonthTemp = $diffMonth - $user['correction_month'];
//					if(!$fulltype && $diffMonthTemp >=7 )$full_pay = 150;
//					unset($diffMonthTemp);
//				}
//				$data['full_pay'] = $full_pay;
//				unset($fulltype);
//				unset($full_pay);			
                //工资总计
                if ($data['personal_leave'] <= 24 && ($diffMonth > 1 || ($diffMonth == 1 && substr($user['employ_time'], 8, 2) > 15))) {//请假三天以内的员工
                    $all_pay = $data['base_pay'] + $data['position_pay'] + $data['seniority_pay'] + $data['insurance_pay'] + $data['overtime_pay'] + $data['night_pay'] + $data['bonus'] + $data['other_bonus'] + $data['absence_pay'];
                    if ($data['personal_leave'] < 8)
                        $all_pay += $data['full_pay'];
                }else {//请假三天以上的员工或新入职的员工
                    if ($data['work_days'] >= 24)
                        $data['work_days'] = $data['work_days'] - round($data['personal_leave'] / 8); //对于work_days没有正常填的，临时处理一下
                    $all_pay = round($data['work_days'] * ($data['base_pay'] + $data['position_pay'] + $data['seniority_pay']) / 24 + $data['insurance_pay'] + $data['overtime_pay'] + $data['night_pay'] + $data['bonus'] + $data['other_bonus']);
                }
                
                //去掉迟到扣钱和漏打卡扣钱
                //漏打卡，扣除一天工资
                if ($data['forget_clock'] > 3){
                    $all_pay = $all_pay - ($data['base_pay'] + $data['position_pay'] + $data['seniority_pay']) / 24;
                }
                //迟到一次扣10元（15分钟以内）
                $all_pay = $all_pay - $data['late'] * 10;

                //阿姨的单独计算，职位为op的
                if ($dataArr2[3] == 'OP')
                    $all_pay = $data['bonus'];

                $data['all_pay'] = round($all_pay);

                unset($all_pay);

                $attendance_save = $this->_tableAttendance->save($data);

                if ($attendance_save)
                    $msg.= 'Code为' . $user['code'] . '的员工工资保存成功<br>';
                else
                    $msg.= 'Code为' . $user['code'] . '的员工工资保存成功<br>';

                unset($attendance_save);
                unset($diffMonth);
                unset($data);
                unset($user);
                unset($employType);
            }
        }else {
            echo "文件上传失败，请联系TBI7";
        }
        echo $msg;
    }

    //根据规则返回工资
    function formulaPay($type, $department, $foreign_key) {
        $fields = array();
        $fields['type'] = $type;
        //类似特殊津贴，是不跟部门挂钩的，所以没有部门值，这里就不能把部门当作条件
        if ($department)
            $fields['department'] = $department;
        $fields['foreign_key'] = $foreign_key;
        return $this->_tableFormula->find($fields);
    }

    //导出财务表
    function actionExport() {
        $yesr = $_REQUEST['year'];
        $month = $_REQUEST['month'];
        $dept_id = $_REQUEST['dept_id'];
        $department = $_REQUEST['department'];
        $fields = array();
        $fields['year'] = $yesr;
        $fields['month'] = $month;
        $fields['dept_id'] = $dept_id;
        $table = & $this->_tableAttendance;
        $rowset = $table->findAll($fields);
//		dump($this->dbo->log);
//		var_dump($rowset[0]);
        $listTitle = $department . '部门' . $yesr . '年' . $month . '月员工工资统计表';

        //读取模板内容
        $template = 'salary.xlsx';
        $path = 'Include/Template/';
        $PHPExcel = $this->readTemplate($template, $path);
        $PHPExcel->getProperties()->setTitle($listTitle);
        $ActSheet = $PHPExcel->getActiveSheet();
        $columnArray = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ');
        //设置格式
        foreach ($columnArray as $column) {
            $objStyle = $ActSheet->getStyle($column . '3');
            $ActSheet->duplicateStyle($objStyle, $column . '4:' . $column . (string) (count($rowset) + 2));
        }
//		$objStyleA3 = $PHPExcel->getActiveSheet()->getStyle('A3');
//		$ActSheet->duplicateStyle($objStyleA3, 'A4:'.'B'.(string)(count($rowset)+2));
//		$objStyleC3 = $PHPExcel->getActiveSheet()->getStyle('C3');
//		$ActSheet->duplicateStyle($objStyleC3, 'C4:'.'AH'.(string)(count($rowset)+2));
//		$objStyleAJ3 = $PHPExcel->getActiveSheet()->getStyle('AJ3');
//		$ActSheet->duplicateStyle($objStyleAJ3, 'AJ4:'.'AJ'.(string)(count($rowset)+2));
        //填充内容
        $ActSheet->setCellValue('I1', $listTitle);
        for ($i = 3; $i <= count($rowset) + 2; $i++) {
            $row = $rowset[$i - 3];
            $rowArray = array();
            if ($row['position']['name'] == 'OP') {
                //阿姨单独处理
                $rowArray[22] = $row['all_pay'];
                $rowArray[23] = 0;
                $rowArray[24] = 0;
                $rowArray[25] = $row['all_pay'];
                $rowArray[27] = 0;
                $rowArray[28] = 0;
                $rowArray[29] = 0;
                $rowArray[30] = 0;
                $rowArray[31] = 0;
                $rowArray[32] = 0;
                $rowArray[33] = 0;
            } else {
                $rowArray[22] = $row['base_pay'];
                $rowArray[23] = $row['seniority_pay'];
                $rowArray[24] = $row['insurance_pay'];
                $rowArray[25] = round($row['base_pay'] + $row['seniority_pay'] + $row['insurance_pay']);
                $rowArray[27] = $row['overtime_pay'];
                $rowArray[28] = $row['night_pay'];
                $rowArray[29] = $row['position_pay'];
                $rowArray[30] = $row['bonus'];
                $rowArray[31] = $row['other_bonus'];
                $rowArray[32] = $row['absence_pay'];
                $rowArray[33] = $row['overtime_pay'] + $row['night_pay'] + $row['full_pay'] + $row['position_pay'] + $row['bonus'] + $row['other_bonus'] + $row['absence_pay'];
            }
            $rowArray[0] = $row['user']['name'];
            $rowArray[1] = $row['user']['code'];
            $rowArray[2] = $row['all_pay'];
            $rowArray[3] = $row['department']['name'];
            $rowArray[4] = $row['position']['name'];
            $rowArray[5] = $row['education']['name'];
            $rowArray[6] = $row['user']['employ_time'];
            $rowArray[7] = $row['user']['permanent_time'];
            $rowArray[8] = $row['employ_month'];
            $rowArray[9] = $row['permanent_month'];
            $rowArray[10] = $row['work_days'];
            $rowArray[11] = $row['night'];
            $rowArray[12] = ''; //没有单独的字段统计晚班加班，都合并到了晚班字段，加一个晚班肯定就少一个白班，所以没有额外的加班工资，只有津贴
            $rowArray[13] = $row['personal_leave'];
            $rowArray[14] = ''; //没有单独的事假，事病假处理方式一致，都统一到了personal_leave中
            $rowArray[15] = $row['absenteeism']; //迟到字段没有值，迟到就算旷工了，这个字段永远都不会有值，除非不想干了，这个有用到了TBI5，两次迟到15分钟以上，记为2次？
            $rowArray[16] = ''; //早退字段，同上
            $rowArray[17] = $row['late'];
            $rowArray[18] = $row['forget_clock'];
            $rowArray[19] = $row['overtime'];
            $rowArray[20] = $row['endowment'];
            $rowArray[21] = $row['medicare'];
            $rowArray[26] = ''; //员工签名			
//			$rowArray[29] = $row['full_pay'];全勤奖取消了			
            $rowArray[34] = $row['description'];

            for ($j = 0; $j < count($columnArray); $j++) {
                $column = $columnArray[$j];
                $rowValue = $rowArray[$j];
                $ActSheet->setCellValue($column . $i, $rowValue);
            }
        }
        $objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
        $xlsxName = $department . '_' . $yesr . '_' . $month . '_salary.xls';
        $objWriter->save(EXPORT_DIR . DS . $xlsxName);
        echo $xlsxName . "保存成功";
    }

    //导出签字表
    function actionExport2() {
        $yesr = $_REQUEST['year'];
        $month = $_REQUEST['month'];
        $dept_id = $_REQUEST['dept_id'];
        $department = $_REQUEST['department'];
        $fields = array();
        $fields['year'] = $yesr;
        $fields['month'] = $month;
        $fields['dept_id'] = $dept_id;
        $table = & $this->_tableAttendance;
        $rowset = $table->findAll($fields);
//		dump($this->dbo->log);
//		var_dump($rowset[0]);
        $listTitle = $department . '部门' . $yesr . '年' . $month . '月员工工资统计表';

        //读取模板内容
        $template = 'signature.xlsx';
        $path = 'Include/Template/';
        $PHPExcel = $this->readTemplate($template, $path);
        $PHPExcel->getProperties()->setTitle($listTitle);
        $ActSheet = $PHPExcel->getActiveSheet();
        $columnArray = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        //设置格式
        foreach ($columnArray as $column) {
            $objStyle = $ActSheet->getStyle($column . '3');
            $ActSheet->duplicateStyle($objStyle, $column . '4:' . $column . (string) (count($rowset) + 2));
        }
//		$objStyleA3 = $PHPExcel->getActiveSheet()->getStyle('A3');
//		$ActSheet->duplicateStyle($objStyleA3, 'A4:'.'B'.(string)(count($rowset)+2));
//		$objStyleC3 = $PHPExcel->getActiveSheet()->getStyle('C3');
//		$ActSheet->duplicateStyle($objStyleC3, 'C4:'.'AH'.(string)(count($rowset)+2));
//		$objStyleAJ3 = $PHPExcel->getActiveSheet()->getStyle('AJ3');
//		$ActSheet->duplicateStyle($objStyleAJ3, 'AJ4:'.'AJ'.(string)(count($rowset)+2));
        //填充内容
        $ActSheet->setCellValue('I1', $listTitle);
        for ($i = 3; $i <= count($rowset) + 2; $i++) {
            $row = $rowset[$i - 3];
            $rowArray = array();
            if ($row['position']['name'] == 'OP') {
                //阿姨单独处理
                $rowArray[21] = $row['all_pay'];
                $rowArray[22] = 0;
                $rowArray[23] = 0;
                $rowArray[24] = $row['all_pay'];
            } else {
                $rowArray[21] = $row['base_pay'];
                $rowArray[22] = $row['seniority_pay'];
                $rowArray[23] = $row['insurance_pay'];
                $rowArray[24] = round($row['base_pay'] + $row['seniority_pay'] + $row['insurance_pay']);
            }
            $rowArray[0] = $row['user']['name'];
            $rowArray[1] = $row['user']['code'];
            $rowArray[2] = $row['department']['name'];
            $rowArray[3] = $row['position']['name'];
            $rowArray[4] = $row['education']['name'];
            $rowArray[5] = $row['user']['employ_time'];
            $rowArray[6] = $row['user']['permanent_time'];
            $rowArray[7] = $row['employ_month'];
            $rowArray[8] = $row['permanent_month'];
            $rowArray[9] = $row['work_days'];
            $rowArray[10] = $row['night'];
            $rowArray[11] = ''; //没有单独的字段统计晚班加班，都合并到了晚班字段，加一个晚班肯定就少一个白班，所以没有额外的加班工资，只有津贴
            $rowArray[12] = $row['personal_leave'];
            $rowArray[13] = ''; //没有单独的事假，事病假处理方式一致，都统一到了personal_leave中
            $rowArray[14] = ''; //迟到字段没有值，迟到就算旷工了，这个字段永远都不会有值，除非不想干了
            $rowArray[15] = ''; //早退字段，同上
            $rowArray[16] = $row['late'];
            $rowArray[17] = $row['forget_clock'];
            $rowArray[18] = $row['overtime'];
            $rowArray[19] = $row['endowment'];
            $rowArray[20] = $row['medicare'];
            $rowArray[25] = ''; //员工签名			

            for ($j = 0; $j < count($columnArray); $j++) {
                $column = $columnArray[$j];
                $rowValue = $rowArray[$j];
                $ActSheet->setCellValue($column . $i, $rowValue);
            }
        }
        $objWriter = new PHPExcel_Writer_Excel5($PHPExcel);
        $xlsxName = $department . '_' . $yesr . '_' . $month . '_signature.xls';
        $objWriter->save(EXPORT_DIR . DS . $xlsxName);
        echo $xlsxName . "保存成功";
    }

    //读取模板返回内容
    function readTemplate($fileName, $path) {
        $filePath = $path . $fileName;
        $PHPExcel = new PHPExcel();
        $PHPReader = new PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                return false;
            }
        }
        return $PHPReader->load($filePath);
    }

    /**
     * 处理图片上传
     */
    function _uploadPicture() {
        $uploader = & FLEA::getSingleton('FLEA_Helper_FileUploader');
        /* @var $uploader FLEA_Helper_FileUploader */

        $errorMessage = "";
        // 检查上传文件是否存在
        if ($uploader->isFileExist('postfile')) {
            // 检查文件扩展名是否是允许上传的类型
            $file = & $uploader->getFile('postfile');
            if (!$file->check(FLEA::getAppInf('photoFileExts'))) {
                $errorMessage = _T('ui_p_invalid_filetype');
            }

            // 上传图片
            __TRY();
            $filetype = $file->getExt(); //获取图片后缀
            $filename = $this->uploadPhoto($file, $filetype);
            $ex = __CATCH();
            if (__IS_EXCEPTION($ex)) {
                $errorMessage = $ex->getMessage();
                break;
            }else
                $errorMessage['filename'] = $filename;
        }

        if ($errorMessage != "" && !is_array($errorMessage)) {
            echo "<script>alert('" . $errorMessage . "');";
            echo "history.back();</script>";
            exit;
        }

        /* $src = FLEA::getAppInf('uploadDir') . DS . $filename;
          $img = UPLOAD_DIR. "/" .$filename;
          include(TPL_DIR . '/BoPagePicman.php'); */
        return $errorMessage;
    }

    //计算两个日期相差 年 月 日
    function DiffDate($date1, $date2) {
        if (strtotime($date1) > strtotime($date2)) {
            $ymd = $date2;
            $date2 = $date1;
            $date1 = $ymd;
        }
        list($y1, $m1, $d1) = explode('-', $date1);
        list($y2, $m2, $d2) = explode('-', $date2);
        $y = $m = $d = $_m = 0;
        $math = ($y2 - $y1) * 12 + $m2 - $m1;
        $y = floor($math / 12);
        $m = intval($math % 12);
        $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
        if ($d < 0) {
            $m -= 1;
            $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
        }
        $m < 0 && $y -= 1;
        return array($y, $m, $d);
    }

    /**
     * 添加图片
     *
     * @param
     * @param FLEA_Helper_UploadFile $file
     *
     * @return boolean
     */
    function uploadPhoto(&$file, $type) {
        $filename = 'TB' . time() . '.' . $type;
        $file->move(FLEA::getAppInf('uploadDir') . DS . $filename);
        return $filename;
    }

}
